package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.DateTimeUtils;
import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.event.DriverRebateEvent;
import com.ybkj.daijia.server.mapper.DriverMapper;
import com.ybkj.daijia.server.mapper.DriverRebatePaymentRecordMapper;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverPaymentsRecord;
import com.ybkj.daijia.server.model.DriverRebatePaymentRecord;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.service.CompanyService;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhyu
 * @date 2019-07-09 10:37:40
 */
@Component
public class DriverRebateEventListener implements ApplicationListener<DriverRebateEvent> {

    private static final Logger logger = LoggerFactory.getLogger(DriverRebateEventListener.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private DriverRebatePaymentRecordMapper driverRebatePaymentRecordMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public void onApplicationEvent(DriverRebateEvent event) {
        logger.debug("driverRebateEvent check :{}", "");
        ReactorVo vo = (ReactorVo) event.getSource();
        Driver driver = vo.getDriver();
        Passenger passenger = vo.getPassenger();
        if (!driver.canTuiGuang()) {
            logger.info("不允许司机 {}获取推广奖励", driver.getId() + " " + driver.getRealname());
            return;
        }
        try {
            createDriverRebateMoney(driver, passenger, false);
        } catch (ParseException e) {
            logger.error("createDriverRebateMoney", e);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void createDriverRebateMoney(Driver driver, Passenger passenger, boolean isSupplement)
        throws ParseException {
        Company company = companyService.findOne(driver.getCompanyId());

        Setting setting = settingUtils.get();

        BigDecimal driverRebateMoney = new BigDecimal(setting.getDriverRebateMoney());

        driver.setRebateMoney(driver.getRebateMoney().add(driverRebateMoney));
        driverMapper.updateDriverRebate(driver.getRebateMoney(), driver.getId());
        DriverRebatePaymentRecord one = new DriverRebatePaymentRecord();
        if (isSupplement) {
            one.setCreated(DateTimeUtils.DATE_FORMAT_DATE.parse(passenger.getCreated()));
            one.setUpdated(DateTimeUtils.DATE_FORMAT_DATE.parse(passenger.getUpdated()));
        }
        one.setMoney(driverRebateMoney);
        one.setAfterMoney(driver.getRebateMoney());
        if (isSupplement) {
            one.setRecordType(DriverPaymentsRecord.RECHARGE_TYPE_REBATE_REWARD_LOSS);
        } else {
            one.setRecordType(DriverRebatePaymentRecord.RECHARGE_TYPE_REBATE);
        }
        one.setDriverId(driver.getId());
        if (isSupplement) {
            one.setMemo("推荐客户【" + passenger.getName() + "】phone[" + passenger.getPhone() + "]奖励"
                + driverRebateMoney.doubleValue() + "元。");
        } else {
            one.setMemo("推荐客户【" + passenger.getName() + "】phone[" + passenger.getPhone() + "]奖励"
                + driverRebateMoney.doubleValue() + "元。");
        }
        one.setOperating(DriverPaymentsRecord.OPERATING_SYS);
        one.setPassengerId(passenger.getId());
        one.setPassengerPhone(passenger.getPhone());
        driverRebatePaymentRecordMapper.insertRecord(one);
        try {

            //发送设备推送
            PushDataV4 pushDataV4 = new PushDataV4();
            pushDataV4.setCode("0X05");
            pushDataV4.setData(passenger.getId());
            pushDataV4.setType("0X00");
            Map<String, String> extra = new HashMap<String, String>();
            extra.put("content", "你已收到推荐奖励:" + driverRebateMoney.doubleValue() + "元");
            pushDataV4.setExtra(extra);

            boolean getuiOnline = false;
            if (StringUtils.isNotBlank(driver.getUserID())) {
                getuiOnline = GeTuiEmployV4.getClientOnline(driver.getUserID(), settingUtils.get());
            }

            if (getuiOnline) {
                GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
            } else {
                if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                    JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                }
            }

        } catch (IOException e) {
            logger.debug("push error : " + e.getMessage());
        }
    }

}
